#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelSwitch, UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink

def topology():
    "Create a network."
    net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )

    print "*** Creating nodes"
    h1 = net.addHost( 'Host1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
    h2 = net.addHost( 'Host2', mac='00:00:00:00:00:02', ip='10.0.0.2/24' )
    h3 = net.addHost( 'Host3', mac='00:00:00:00:00:03', ip='10.0.0.3/24' )
    h4 = net.addHost( 'Host4', mac='00:00:00:00:00:04', ip='10.0.0.4/24' )
    h5 = net.addHost( 'Host5', mac='00:00:00:00:00:05', ip='10.0.0.5/24' )
    h6 = net.addHost( 'Host6', mac='00:00:00:00:00:06', ip='10.0.0.6/24' )
    h7 = net.addHost( 'Host7', mac='00:00:00:00:00:07', ip='10.0.0.7/24' )
    h8 = net.addHost( 'Host8', mac='00:00:00:00:00:08', ip='10.0.0.8/24' )
    
    # s4 = net.addSwitch( 's4', listenPort=6673, mac='00:00:00:00:00:04' )
    # c7 = net.addController( 'c7', controller=RemoteController, ip='127.0.0.1', port=6633 )
    s1 = net.addSwitch('Switch1')
    s2 = net.addSwitch('Switch2')
    s3 = net.addSwitch('Switch3')
    s4 = net.addSwitch('Switch4')
    s5 = net.addSwitch('Switch5')
    s6 = net.addSwitch('Switch6')
    s7 = net.addSwitch('Switch7')
    s8 = net.addSwitch('Switch8')
    s9 = net.addSwitch('Switch9')
    s10 = net.addSwitch('Switch10')
    s11 = net.addSwitch('Switch11')
    s12 = net.addSwitch('Switch12')
    c1 = net.addController('c1', controller=RemoteController, ip='127.0.0.1', port=6633)

    print "*** Creating links"
    # net.addLink(h1, s4)
    # net.addLink(s4, h2)
    net.addLink(h1,s9)
    net.addLink(h2,s9)
    net.addLink(h3,s10)
    net.addLink(h4,s10)
    net.addLink(h5,s11)
    net.addLink(h6,s11)
    net.addLink(h7,s12)
    net.addLink(h8,s12)
    net.addLink(s1,s5)
    net.addLink(s1,s7)
    net.addLink(s2,s5)
    net.addLink(s2,s7)
    net.addLink(s3,s6)
    net.addLink(s3,s8)
    net.addLink(s4,s6)
    net.addLink(s4,s8)
    net.addLink(s5,s10)
    net.addLink(s5,s9)
    net.addLink(s6,s10)
    net.addLink(s6,s9)
    net.addLink(s7,s11)
    net.addLink(s7,s12)
    net.addLink(s8,s11)
    net.addLink(s8,s12)

    print "*** Starting network"
    net.build()
    c1.start()
    s1.start( [c1] )
    s2.start( [c1] )
    s3.start( [c1] )
    s4.start( [c1] )
    s5.start( [c1] )
    s6.start( [c1] )
    s7.start( [c1] )
    s8.start( [c1] )
    s9.start( [c1] )
    s10.start( [c1] )
    s11.start( [c1] )
    s12.start( [c1] )
    
    # s4.cmd("sysctl net.ipv6.conf.all.disable_ipv6=1")
    h1.cmd("ifconfig Host1-eth0 inet6 add fc00::1/64")
    h2.cmd("ifconfig Host2-eth0 inet6 add fc00::2/64")
    h3.cmd("ifconfig Host3-eth0 inet6 add fc00::3/64")
    h4.cmd("ifconfig Host4-eth0 inet6 add fc00::4/64")
    h5.cmd("ifconfig Host5-eth0 inet6 add fc00::5/64")
    h6.cmd("ifconfig Host6-eth0 inet6 add fc00::6/64")
    h7.cmd("ifconfig Host7-eth0 inet6 add fc00::7/64")
    h8.cmd("ifconfig Host8-eth0 inet6 add fc00::8/64")
    
    # h1.cmdPrint("ping -c 3 10.0.0.2")
    # h1.cmdPrint("ping6 -c 3 fc00::2 -I h1-eth0")
    # h1.cmdPrint("route -6 -n")
    # h1.cmdPrint("ip -6 neighbor show")

    print "*** Running CLI"
    CLI( net )

    print "*** Stopping network"
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    topology()
